﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Sales;
using ESOL.Business.Fin;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.Pur;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Finance
{
    public partial class frmManageAPInvoice : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private FinBillingEntity mSelectedBilling;
        #endregion

        #region Constructor
        public frmManageAPInvoice()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", true);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colCURRENCY, "CS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colPAY_TYPE, "PAT", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "STATUS", true);
        }
        private string GetCondition()
        {
            string condition = "";
            if (txtSupplier.Tag != null && txtSupplier.Tag.GetType() == typeof(MstSupplierEntity))
            {
                MstSupplierEntity supplier = (MstSupplierEntity)txtSupplier.Tag;
                condition += " AND S.SUPPLIER_ID = '" + supplier.SupplierId + "' ";
            }
            if (string.IsNullOrEmpty(txtInvoiceNo.Text) == false)
            {
                condition += " AND B.INVOICE_NO LIKE '%" + txtInvoiceNo.Text + "%'";
            }
            if (txtBillingPerson.Tag != null && txtBillingPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtBillingPerson.Tag;
                condition += " AND B.BILLING_PERSON = '" + entity.PersonId + "' ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND B.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            else
            {
                condition += " AND B.STATUS != 'STATUS05'";
            }
            if (dtpBillingDate.EditValue!=null)
            {
                condition += " AND B.BILLING_DATE >= CONVERT(DATETIME,'" + dtpBillingDate.EditValue.ToString() + "')";
            }
            if (dtpBillingDateTo.EditValue != null)
            {
                condition += " AND B.BILLING_DATE <= CONVERT(DATETIME,'" + dtpBillingDateTo.EditValue.ToString() + "')";
            }
            if (string.IsNullOrEmpty(txtBillingCode.Text) == false)
            {
                condition += " AND B.BILLING_CODE LIKE '%" + txtBillingCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(cmbPaidStatus.Text) == false)
            {
                if (cmbPaidStatus.Text == "已付款")
                {
                    condition += " AND PD.BILLING_ID IS NOT NULL ";
                }
                else
                {
                    condition += " AND PD.BILLING_ID IS NULL ";
                }
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "B.BILLING_CODE DESC";
            DataTable dt = FinBillingBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        private void ResetButtonsEnable()
        {
            btnModify.Enabled = false;
            btnDelete.Enabled = false;
            btnSubmit.Enabled = false;
            btnApprove.Enabled = false;
            btnInvalid.Enabled = false;
            if (mSelectedBilling == null)
            {
                return;
            }
            if (mSelectedBilling.Status == "STATUS02"
                || mSelectedBilling.Status == "STATUS04")
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                btnApprove.Enabled = true;
            }
            else if (mSelectedBilling.Status == "STATUS03")
            {
                btnInvalid.Enabled = true;
            }
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComboBox();
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedBilling = new FinBillingEntity(dr);
            ResetButtonsEnable();
        }

        protected override void Click_View()
        {
            if (mSelectedBilling == null)
            {
                MessageBox.Show("请选择一个发票录入单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAPInvoice frm = new frmMaintainAPInvoice();
            frm.ThisActionType = ActionType.View;
            frm.SelectedBilling = mSelectedBilling;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedBilling == null)
            {
                MessageBox.Show("请选择一个发票录入单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中发票录入单？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedBilling.Status = "STATUS05";
                mSelectedBilling.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedBilling.LastModifiedDate = DateTime.Now;
                bool successful = FinBillingBs.UpdateFinBilling(mSelectedBilling);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedBilling == null)
            {
                MessageBox.Show("请选择一个发票录入单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAPInvoice frm = new frmMaintainAPInvoice();
            frm.ThisActionType = ActionType.Approve;
            frm.SelectedBilling = mSelectedBilling;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedBilling == null)
            {
                MessageBox.Show("请选择一个发票录入单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAPInvoice frm = new frmMaintainAPInvoice();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedBilling = mSelectedBilling;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {
            frmMaintainAPInvoice frm = new frmMaintainAPInvoice();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void txtBillingPerson_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
                if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
                {
                    txtBillingPerson.Text = frm.SelectedPerson.NameCn;
                    txtBillingPerson.Tag = frm.SelectedPerson;
                }
                else
                {
                    txtBillingPerson.Text = "";
                    txtBillingPerson.Tag = null;
                }
            }
        }


        private void txtSupplier_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainSupplierQuery frm = new frmMaintainSupplierQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedSupplier != null)
            {
                txtSupplier.Text = frm.SelectedSupplier.SupplierName;
                txtSupplier.Tag = frm.SelectedSupplier;
            }
            else
            {
                txtSupplier.Text = "";
                txtSupplier.Tag = null;
            }
        }

        private void txtBillingPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (mSelectedBilling == null)
            {
                MessageBox.Show("请选择一个发票录入单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (mSelectedBilling.Status != "STATUS03")
            {
                MessageBox.Show("请选择一个已审的发票录入单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否确定作废？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedBilling.Status = "STATUS99";
                mSelectedBilling.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedBilling.LastModifiedDate = DateTime.Now;
                bool successful = FinBillingBs.InvalidBilling(mSelectedBilling);
                if (successful)
                {
                    MessageBox.Show("作废成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        #endregion


    }
}
